package com.baohulu.baohulu.activity.account;

import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.view.View;

import com.baohulu.baohulu.R;
import com.baohulu.baohulu.base.BaseResponseCallback;
import com.baohulu.baohulu.common.Const;
import com.baohulu.baohulu.utils.HttpUtil;
import com.baohulu.baohulu.utils.JsonUtil;
import com.baohulu.baohulu.utils.ToastUtil;

import org.json.JSONException;
import org.json.JSONObject;

public class RegisterState extends AccountState {
    RegisterState(AccountActivity activity) {
        super(activity);
    }

    @Override
    public void init() {
        mActivity.tvConfirmPassword.setVisibility(View.VISIBLE);
        mActivity.etConfirmPassword.setVisibility(View.VISIBLE);
        mActivity.tvForgetPassword.setVisibility(View.INVISIBLE);
        mActivity.tvHintText.setText(R.string.user_register);
        mActivity.btnLogin.setText(R.string.register);
        mActivity.tvRegisterAcc.setText(R.string.back_to_login);
        //清空输入
        clearInput();
    }

    private void clearInput() {
        mActivity.etPhoneNumber.setText("");
        mActivity.etPassword.setText("");
        mActivity.etConfirmPassword.setText("");
    }

    @Override
    public boolean isInputLegal() {
        phoneNumber = mActivity.etPhoneNumber.getText().toString();
        password = mActivity.etPassword.getText().toString();
        String confirmPassword = mActivity.etConfirmPassword.getText().toString();
        if (TextUtils.isEmpty(phoneNumber) || TextUtils.isEmpty(password) || TextUtils.isEmpty(confirmPassword)) {
            ToastUtil.showToast("用户名/密码不能为空！");
        } else if (isPhoneNumberInvalid(phoneNumber)) {
            ToastUtil.showToast("手机号码不合法");
        } else if (password.length() < 6) {
            ToastUtil.showToast("密码不能小于6位");
        } else if (!confirmPassword.equals(password)) {
            ToastUtil.showToast("两次输入密码不一致！");
        } else {
            return true;
        }
        return false;
    }

    @Override
    public void submitData() {
        String loginUrl = mActivity.getString(R.string.url_base) + mActivity.getString(R.string.url_user_register);
        JSONObject data = new JsonUtil.JsonBuilder().put("account", phoneNumber)
                .put("password", password).build();

        HttpUtil.post(mActivity, loginUrl, data, new BaseResponseCallback() {
            @Override
            public void onStart() {
                mActivity.showProgressDialog();
            }

            @Override
            public void onJsonObjectResponse(JSONObject jsonObject) {
                super.onJsonObjectResponse(jsonObject);
                try {
                    int state = jsonObject.getInt("state");
                    resolveRegisterResult(state);
                } catch (JSONException e) {
                    e.printStackTrace();
                } finally {
                    mActivity.hideProgressDialog();
                }
            }

            @Override
            public void onFailure(final String msg) {
                mActivity.hideProgressDialog();
                ToastUtil.showToast(msg);
            }
        });

    }

    /**
     * 处理用户注册结果
     *
     * @param state 服务端返回状态码
     */
    private void resolveRegisterResult(int state) {
        if (state == Const.ResponseState.SUCCESS) {
            //注册成功
            showRegisterSuccessDialog();
        } else {
            //注册失败
            ToastUtil.showToast(R.string.account_existed);
        }
    }

    /**
     * 注册成功后的提示对话框
     */
    private void showRegisterSuccessDialog() {
        new AlertDialog.Builder(mActivity)
                .setMessage(R.string.register_success)
                .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        mActivity.toggleState();
                        dialog.dismiss();
                    }
                })
                .setCancelable(false)
                .create()
                .show();
    }
}
